rm(list=ls())
library(foreign)
library(dplyr)
library(ggplot2)
#library(animation)
library(spatialEco)
#library(scales)
library(gtable)
library(grid)
library(readstata13)

mydata <- read.dta("Drought_final.dta")
Voteshare_inc <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_inc!="")
Voteshare_pm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_pm!="")
Voteshare_cm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_cm!="")
Voteshare_inc_cm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_inc_cm!="")
Voteshare_inc_split <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_inc_noncm!="")
Voteshare_inc_pm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_inc_pm!="")
Voteshare_inc_split2 <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_inc_nonpm!="")
Turnout <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_Turnout!="")

#Figure 2-A
lo.Voteshare_inc.f <- loess.boot(Voteshare_inc$demean_prev_monsoon2,Voteshare_inc$demean_inc, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc<-data.frame(lo.Voteshare_inc.f$fit)

bs_Voteshare_inc<-ggplot(aes(x = lo.Voteshare_inc$x, y = lo.Voteshare_inc$y.fit), data = lo.Voteshare_inc) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc, aes(ymin=lo.Voteshare_inc$low.lim,ymax=lo.Voteshare_inc$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc<-ggplot(data=Voteshare_inc, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)



#Figure 2-b
fit <- lm(demean_inc ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_inc)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_inc$demean_prev_monsoon2)[1], to = range(Voteshare_inc$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_inc$up.lim)) + geom_line(aes(y = lo.Voteshare_inc$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc<-ggplot(data=Voteshare_inc, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

############################
##Figure 2-c
############################

lo.Voteshare_pm.f <- loess.boot(Voteshare_pm$demean_prev_monsoon2,Voteshare_pm$demean_pm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_pm<-data.frame(lo.Voteshare_pm.f$fit)

bs_Voteshare_pm<-ggplot(aes(x = lo.Voteshare_pm$x, y = lo.Voteshare_pm$y.fit), data = lo.Voteshare_pm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_pm_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_pm_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_pm, aes(ymin=lo.Voteshare_pm$low.lim,ymax=lo.Voteshare_pm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (PM)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_pm<-ggplot(data=Voteshare_pm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

##Figure 2-d
fit <- lm(demean_pm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_pm)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_pm$demean_prev_monsoon2)[1], to = range(Voteshare_pm$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_pm<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_pm$up.lim)) + geom_line(aes(y = lo.Voteshare_pm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (PM)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_pm<-ggplot(data=Voteshare_pm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

############################
##Figure 2-e
############################

lo.Voteshare_cm.f <- loess.boot(Voteshare_cm$demean_prev_monsoon2,Voteshare_cm$demean_cm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_cm<-data.frame(lo.Voteshare_cm.f$fit)

bs_Voteshare_cm<-ggplot(aes(x = lo.Voteshare_cm$x, y = lo.Voteshare_cm$y.fit), data = lo.Voteshare_cm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_cm_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_cm_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_cm, aes(ymin=lo.Voteshare_cm$low.lim,ymax=lo.Voteshare_cm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (CM)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_cm<-ggplot(data=Voteshare_cm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

###Figure 2-f
fit <- lm(demean_cm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_cm)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_cm$demean_prev_monsoon2)[1], to = range(Voteshare_cm$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_cm<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_cm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (CM)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_cm<-ggplot(data=Voteshare_cm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)


############################
##Figure 3-a
############################

lo.Voteshare_inc_pm.f <- loess.boot(Voteshare_inc_pm$demean_prev_monsoon2,Voteshare_inc_pm$demean_inc_pm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_pm<-data.frame(lo.Voteshare_inc_pm.f$fit)

###LOWESS

bs_Voteshare_inc_pm<-ggplot(aes(x = lo.Voteshare_inc_pm$x, y = lo.Voteshare_inc_pm$y.fit), data = lo.Voteshare_inc_pm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_pm, aes(ymin=lo.Voteshare_inc_pm$low.lim,ymax=lo.Voteshare_inc_pm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (PM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_pm<-ggplot(data=Voteshare_inc_pm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)


##Figure 3-b
lo.Voteshare_inc_split2.f <- loess.boot(Voteshare_inc_split2$demean_prev_monsoon2,Voteshare_inc_split2$demean_inc_nonpm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_split2<-data.frame(lo.Voteshare_inc_split2.f$fit)

bs_Voteshare_inc_split2<-ggplot(aes(x = lo.Voteshare_inc_split2$x, y = lo.Voteshare_inc_split2$y.fit), data = lo.Voteshare_inc_split2) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split2$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split2$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_split2, aes(ymin=lo.Voteshare_inc_split2$low.lim,ymax=lo.Voteshare_inc_split2$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Non-PM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split2<-ggplot(data=Voteshare_inc_split2, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 



g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_split2))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split2))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)



##Figure 3-c
fit <- lm(demean_inc_pm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_inc_pm)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_inc_pm$demean_prev_monsoon2)[1], to = range(Voteshare_inc_pm$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_pm<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_pm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_pm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (PM-Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_pm<-ggplot(data=Voteshare_inc_pm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it


##Figure 3-d

fit <- lm(demean_inc_nonpm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_inc_split2)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_inc_split2$demean_prev_monsoon2)[1], to = range(Voteshare_inc_split2$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_split2<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split2$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split2$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-PM Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Non-PM Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split2<-ggplot(data=Voteshare_inc_split2, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_split2))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split2))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it



############################
###Figure 4-a
############################


#####LOWESS
lo.Voteshare_inc_cm.f <- loess.boot(Voteshare_inc_cm$demean_prev_monsoon2,Voteshare_inc_cm$demean_inc_cm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_cm<-data.frame(lo.Voteshare_inc_cm.f$fit)

bs_Voteshare_inc_cm<-ggplot(aes(x = lo.Voteshare_inc_cm$x, y = lo.Voteshare_inc_cm$y.fit), data = lo.Voteshare_inc_cm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_cm, aes(ymin=lo.Voteshare_inc_cm$low.lim,ymax=lo.Voteshare_inc_cm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (CM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_cm<-ggplot(data=Voteshare_inc_cm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 



g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)


#Figure 4-b
lo.Voteshare_inc_split.f <- loess.boot(Voteshare_inc_split$demean_prev_monsoon2,Voteshare_inc_split$demean_inc_noncm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_split<-data.frame(lo.Voteshare_inc_split.f$fit)

bs_Voteshare_inc_split<-ggplot(aes(x = lo.Voteshare_inc_split$x, y = lo.Voteshare_inc_split$y.fit), data = lo.Voteshare_inc_split) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_split, aes(ymin=lo.Voteshare_inc_split$low.lim,ymax=lo.Voteshare_inc_split$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Non-CM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split<-ggplot(data=Voteshare_inc_split, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_split))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)



##Figure 4-c
fit <- lm(demean_inc_cm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_inc_cm)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_inc_cm$demean_prev_monsoon2)[1], to = range(Voteshare_inc_cm$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_cm<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (CM-Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_cm<-ggplot(data=Voteshare_inc_cm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it


#Figure 4-d
fit <- lm(demean_inc_noncm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_inc_split)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_inc_split$demean_prev_monsoon2)[1], to = range(Voteshare_inc_split$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_split<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-CM Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Non-CM Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split<-ggplot(data=Voteshare_inc_split, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_split))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it


############################
##Figure 5-a
############################

lo.Turnout.f <- loess.boot(Turnout$demean_prev_monsoon2,Turnout$demean_Turnout, nreps=1000, confidence = 0.95,span=0.1)
lo.Turnout<-data.frame(lo.Turnout.f$fit)

bs_Turnout<-ggplot(aes(x = lo.Turnout$x, y = lo.Turnout$y.fit), data = lo.Turnout) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Turnout_cm$up.lim)) + geom_line(aes(y = lo.Turnout_cm$low.lim)) +
  geom_ribbon(data=lo.Turnout, aes(ymin=lo.Turnout$low.lim,ymax=lo.Turnout$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Turnout (demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Turnout") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Turnout<-ggplot(data=Turnout, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Turnout))
g2 <- ggplot_gtable(ggplot_build(gg_Turnout))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)


##Figure 5-b
fit <- lm(demean_Turnout ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Turnout)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Turnout$demean_prev_monsoon2)[1], to = range(Turnout$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Turnout<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Turnout (demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Turnout") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Turnout<-ggplot(data=Turnout, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Turnout))
g2 <- ggplot_gtable(ggplot_build(gg_Turnout))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it
grid.draw(g)


